<?php 
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of IndexController
*
*/

class UserController extends Zend_Controller_Action
{

	public function init(){
		$registry = Zend_Registry::getInstance();
		$this->_db = $registry->get('db');
	}
	 
    public function indexAction()
    {
		$this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/default/models/User.php');
        $request = $this->getRequest();
        $module = $request->getParam('module');
		$user_model = new User();
		$view_users = $user_model->viewUsers();
        $this->view->view_users = $view_users;
    }   
	
	public function adduserAction()
	{
		$this->_helper->layout()->disableLayout();
	    Zend_Loader::loadFile('../application/default/forms/User.php');
		Zend_Loader::loadFile('../application/default/models/User.php');
		$user_model = new User();
		$request = $this->getRequest();
		$user_form = new Form_User();
		$this->view->user_form = $user_form;
	}
	
	public function doadduserAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/default/forms/User.php');
		Zend_Loader::loadFile('../application/default/models/User.php');
		$form_user = new Form_User();
		$request = $this->getRequest();
		
		if ($this->getRequest()->isPost()) {            
			$user_model = new User();
			$form_values = $request->getPost();
			$add_user = $user_model->addUser($form_values);
		}
		
		exit;
	}

	public function editAction()
	{
		$this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/default/forms/User.php');
        Zend_Loader::loadFile('../application/default/models/User.php');
        
        $user_model = new User();
        $controller = $this->getRequest()->getControllerName();
		$action = $this->getRequest()->getActionName();
		$userid = $this->_getParam('userid', 0);
		$user_info_arr = $user_model->viewUsersById($userid);
		
		$first_name = $user_info_arr['first_name'];
		$last_name = $user_info_arr['last_name'];
		$email_address = $user_info_arr['email_address'];
		$create_date = $user_info_arr['created_date'];
		$is_online = $user_info_arr['is_online'];
		$userlevel = $user_info_arr['userlevel'];
		$status = $user_info_arr['status'];
		
		$options = array(
			"action" => $action, 
			"userid" => $userid, 
			"first_name" => $first_name,
			"last_name" => $last_name,
			"email_address" => $email_address,
			"create_date" => $create_date,
			"userlevel" => $userlevel,
			"status" => $status);
		
		$user_form = new Form_User($options);
        $this->view->user_form = $user_form;        
        
        $this->view->userid = $userid;
        if ($userid > 0) {            
            $user_form->populate($options);                
        }	
	}
	
	public function doedituserAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/default/forms/User.php');
		Zend_Loader::loadFile('../application/default/models/User.php');		
		$form_user = new Form_User();
		$request = $this->getRequest();
		if ($this->getRequest()->isPost()) {            
			$user_model = new User();
			$form_values = $request->getPost();
			$update_user = $user_model->updateUser($form_values);			
		}
		
		exit;
	}
	
	public function deleteuserAction()
	{
		$this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/default/models/User.php');	
		$request = $this->getRequest();
		$userid = $request->getParam('userid', 0);
		$user_model = new User();
		$del_user = $user_model->deleteUser($userid);
		//$this->_helper->redirector('index');
	}
	
	/*public function doeditpwdAction()
	{
        $this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/admin/models/User.php');	
	    $request = $this->getRequest();
	    $userid = $request->getParam('userid');
	    $curr_pwd = $request->getParam('curr_pwd');
	    $new_pwd = $request->getParam('new_pwd');
        $mod_user = new User();
        $change_pwd = $mod_user->updateAdminPassword($userid, $curr_pwd, $new_pwd);	   
    }*/
    
    public function projectAction()
    {
    	$this->_helper->layout()->disableLayout();
    }
    
    public function editprofileAction()
	{
		$this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/default/forms/User.php');
        Zend_Loader::loadFile('../application/default/models/User.php');
        Zend_Loader::loadFile('../application/default/models/Common.php');
        
        $user_model = new User();
        $common_model = new Common();
        $request = $this->getRequest();
        $action = $this->getRequest()->getActionName();
		$userid = $this->_getParam('userid', 0);
        
		$user_info_arr = $user_model->viewUsersById($userid);
		
		$first_name = $user_info_arr['first_name'];
		$last_name = $user_info_arr['last_name'];
		$email_address = $user_info_arr['email_address'];
		$create_date = $user_info_arr['created_date'];
		$is_online = $user_info_arr['is_online'];
		$userlevel = $user_info_arr['userlevel'];
		$status = $user_info_arr['status'];
		$profile_image = $user_info_arr['profile_image'];
		
		$options = array(
			"action" => $action, 
			"userid" => $userid, 
			"first_name" => $first_name,
			"last_name" => $last_name,
			"email_address" => $email_address,
			"create_date" => $create_date,
			"userlevel" => $userlevel,
			"status" => $status);
		
		$user_form = new Form_User($options);
        $this->view->user_form = $user_form;        
        
        $this->view->userid = $userid;
        if ($userid > 0) {            
            $user_form->populate($options);                
        }
        $target_path = "/images/profileimg/large/" . $profile_image;	
        
        $this->view->profile_image = $target_path;
	} 
	
	public function doupdateuserprofileAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/default/forms/User.php');
        Zend_Loader::loadFile('../application/default/models/User.php');
        
        $form_user = new Form_User();
		$request = $this->getRequest();
		if ($this->getRequest()->isPost()) {            
			$user_model = new User();
			$form_values = $request->getPost();
			$update_user = $user_model->updateUserProfile($form_values);			
		}
	}
	
	public function addimageAction()
	{
		$this->_helper->layout()->disableLayout();	
		$this->view->userid = Zend_Auth::getInstance()->getStorage()->read()->userid;
	}
	
	public function addthemeAction()
	{
		$this->_helper->layout()->disableLayout();	
		$this->view->userid = Zend_Auth::getInstance()->getStorage()->read()->userid;
	}
	
	public function addprofileimageAction()
    {	
		$error = "";
		$msg = "";
		$fileElementName = 'profile_img';		
		
		Zend_Loader::loadFile('../application/default/models/User.php');
		Zend_Loader::loadFile('../application/default/models/Common.php');
		
		$user_model = new User();
		$common_model = new Common();
		$request = $this->getRequest();
		$user_id = $request->getParam('cust_id', 0);
		
		if(!empty($_FILES[$fileElementName]['error']))
		{
			switch($_FILES[$fileElementName]['error'])
			{
	
				case '1':
					$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
					break;
				case '2':
					$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
					break;
				case '3':
					$error = 'The uploaded file was only partially uploaded';
					break;
				case '4':
					$error = 'No file was uploaded.';
					break;
	
				case '6':
					$error = 'Missing a temporary folder';
					break;
				case '7':
					$error = 'Failed to write file to disk';
					break;
				case '8':
					$error = 'File upload stopped by extension';
					break;
				case '999':
				default:
					$error = 'No error code avaiable';
			}
		}
		elseif(empty($_FILES['profile_img']['tmp_name']) || $_FILES['profile_img']['tmp_name'] == 'none')
		{
			$error = 'No file was uploaded..';
		} else {
			$target_path = "images/profileimg/";
			$target_thumb_path = "images/profileimg/thumbs/";
			$target_large_path = "images/profileimg/large/";
	
			$target_path = $target_path . $_FILES['profile_img']['name']; 
			$target_thumb_path = $target_thumb_path . $_FILES['profile_img']['name']; 
			$target_large_path = $target_large_path . $_FILES['profile_img']['name']; 
			move_uploaded_file($_FILES['profile_img']['tmp_name'], $target_path);
			$user_model->addProfileImage($_FILES['profile_img']['name'], $user_id);
			/*$im = $common_model->thumbnail($target_path, 140);			
    		$common_model->imageToFile($im, $target_thumb_path);*/
			$large_image = $common_model->thumbnail($target_path, 400);
			$common_model->imageToFile($large_image, $target_large_path);
			
			$thumbnail = $common_model->thumbnail($target_path, 140);
			$common_model->imageToFile($thumbnail, $target_thumb_path);
		}
		
		exit;
    }
    
    // add theme image
    
    public function addthemeimageAction()
    {	
		$error = "";
		$msg = "";
		$fileElementName = 'theme_img';		
		
		Zend_Loader::loadFile('../application/default/models/User.php');
		Zend_Loader::loadFile('../application/default/models/Common.php');
		
		$user_model = new User();
		$common_model = new Common();
		$request = $this->getRequest();
		$user_id = $request->getParam('cust_id', 0);
		
		if(!empty($_FILES[$fileElementName]['error']))
		{
			switch($_FILES[$fileElementName]['error'])
			{
	
				case '1':
					$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
					break;
				case '2':
					$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
					break;
				case '3':
					$error = 'The uploaded file was only partially uploaded';
					break;
				case '4':
					$error = 'No file was uploaded.';
					break;	
				case '6':
					$error = 'Missing a temporary folder';
					break;
				case '7':
					$error = 'Failed to write file to disk';
					break;
				case '8':
					$error = 'File upload stopped by extension';
					break;
				case '999':
				default:
					$error = 'No error code avaiable';
			}
		}
		elseif(empty($_FILES['theme_img']['tmp_name']) || $_FILES['theme_img']['tmp_name'] == 'none')
		{
			$error = 'No file was uploaded..';
		} else {
			$target_path = "images/themeimage/";
			//$target_thumb_path = "images/themeimage/thumbs/";			
	
			$target_path = $target_path . $_FILES['theme_img']['name'];
			move_uploaded_file($_FILES['theme_img']['tmp_name'], $target_path);
			$user_model->addThemeImage($_FILES['theme_img']['name'], $user_id);			
		}
		
		exit;
    }
    
   // end
    
    public function delprofileimageAction()
    {
    	$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/default/models/User.php');
		$model_user = new User();
		
		$request = $this->getRequest();
		$userid = $request->getParam('userid');
		$del_profile_img = $model_user->delProfileImage($userid);
    }
    
    public function updatepasswordAction()
    {
    	$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/default/models/User.php');
		
		$request = $this->getRequest();
		$userid = $request->getParam('userid');
		$curr_pass = $request->getParam('curr_pass');
		$new_pass = $request->getParam('new_pass');
		$curr_pass = md5($curr_pass);
		$new_pass = md5($new_pass);

		$user_model = new User();
		
		$change_pass = $user_model->updatePassword($userid, $curr_pass, $new_pass);
    }
    
    public function resetpasswordAction()
    {
    	$this->_helper->layout()->disableLayout();
    	Zend_Loader::loadFile('../application/default/models/User.php');
    	$request = $this->getRequest();
    	if ($request->isPost()) {            
			$user_model = new User();
			$form_values = $request->getPost();
			$update_user = $user_model->resetPasswordByEmail($form_values);			
		}
    }
    
    public function delthemeimageAction()
    {
    	$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/default/models/User.php');
		$model_user = new User();
		
		$request = $this->getRequest();
		$userid = $request->getParam('userid');
		$del_theme_img = $model_user->delThemeImage($userid);
    }
}
?>
